package com.youlai.system.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.system.converter.CesConverter;
import com.youlai.system.mapper.CesMapper;
import com.youlai.system.model.bo.CesBO;
import com.youlai.system.model.entity.Ces;
import com.youlai.system.model.form.CesForm;
import com.youlai.system.model.query.CesPageQuery;
import com.youlai.system.model.vo.CesPageVO;
import com.youlai.system.service.CesService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class CesServiceImpl extends ServiceImpl<CesMapper, Ces> implements CesService {
    private final CesConverter cesConverter;

    /**
     * 获取用户分页列表
     *
     * @param queryParams
     * @return
     */
    @Override
    public IPage<CesPageVO> getCesPage(CesPageQuery queryParams) {

        // 参数构建
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        Page<CesBO> page = new Page<>(pageNum, pageSize);

        // 查询数据
        Page<CesBO> cesBOPage = this.baseMapper.getCesPage(page, queryParams);

        // 实体转换
        Page<CesPageVO> cesPageVOPage = cesConverter.bo2Vo(cesBOPage);

        return cesPageVOPage;
    }
    /**
     * 删除用户
     *
     * @param idsStr 用户ID，多个以英文逗号(,)分割
     * @return
     */
    @Override
    public boolean deleteCess(String idsStr) {
        Assert.isTrue(StrUtil.isNotBlank(idsStr), "删除的用户数据为空");
        // 逻辑删除
        List<Long> ids = Arrays.asList(idsStr.split(",")).stream()
                .map(idStr -> Long.parseLong(idStr)).collect(Collectors.toList());
        boolean result = this.removeByIds(ids);
        return result;

    }
    @Override
    public boolean saveDict(CesForm dictForm) {
        // 实体对象转换 form->entity
        Ces entity = cesConverter.form2Entity(dictForm);
        // 持久化
        boolean result = this.save(entity);
        return result;
    }
}
